package com.example.criminalintent.util

import android.app.Activity
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Point
import kotlin.math.roundToInt

object PictureUtils {
    fun getScaledBitmap(path: String, destWidth: Int, destHeight: Int): Bitmap {
        var options = BitmapFactory.Options()
        options.inJustDecodeBounds = true
        BitmapFactory.decodeFile(path, options)
        val srcHeight = options.outHeight.toFloat()
        val srcWidth = options.outWidth.toFloat()
        var inSampleSize = 1
        if (srcHeight > destHeight || srcWidth > destWidth) {
            val heightScale: Float = (srcHeight / destHeight)
            val widthScale: Float = (srcWidth / destWidth)
            inSampleSize =
                if (heightScale > widthScale) heightScale.roundToInt() else widthScale.roundToInt()
        }
        options = BitmapFactory.Options()
        options.inSampleSize = inSampleSize

        return BitmapFactory.decodeFile(path, options)
    }

    fun getScaledBitmap(path: String, activity: Activity): Bitmap {
        val size = Point()
        activity.windowManager.defaultDisplay.getSize(size)
        return getScaledBitmap(path, size.x, size.y)
    }
}